/*
 * v1.1 by tonydon site:txdnet.cn
 * netbeansIDE 7.1
 */
package cn.txdnet.txcms.admin.action;

import cn.txdnet.txcms.domain.Catalogs;
import cn.txdnet.txcms.domain.Manager;
import cn.txdnet.txcms.service.CatalogsService;
import cn.txdnet.txweb.model.ModelUtil;
import cn.txdnet.txweb.model.ModelView;
import cn.txdnet.txweb.utils.BeanUtil;
import cn.txdnet.txweb.utils.Message;
import cn.txdnet.txweb.utils.SessionUtil;
import cn.txdnet.txweb.utils.WebUtil;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
 *
 * @author txdnet
 */
public class CatalogsAction {
        
    // 建立新分类
     public ModelView make(HttpServletRequest req)throws Exception{
          Catalogs ct = BeanUtil.parseRequestToBean(Catalogs.class, req);
       if(CatalogsService.makeCatalogs(ct)){
          return ModelUtil.getTextModel(Message.MSG_SUCCESS);
       }else{
          return ModelUtil.getTextModel(Message.MSG_FAIL);
       }
     }
     
//     分类列表输出
     public ModelView list_factor(HttpServletRequest req)throws Exception{
         int rid = WebUtil.getInt(req, "rid", 0);
         byte isopen = WebUtil.getByte(req, "isopen", -1);
         byte infotype = WebUtil.getByte(req, "infotype", -1);
         String ordnum = WebUtil.getNoNull(req, "ordnum", null);
         String colfilter = WebUtil.getNoNull(req, "colfilter", null);
         String ztree = WebUtil.getNoNull(req, "ztree", null);
         String all = WebUtil.getNoNull(req, "all", null);
         
         Map<String,Object> m = new HashMap<String,Object>();
         // 查找该管理员下可管理的栏目信息,超级管理员可以管理所有栏目信息不进行栏目筛选，否则一律输出指派栏目
          Manager mgr = SessionUtil.getManager(req);
          if(mgr.getMgrid()>1){
                 m.put("cids", mgr.getCids()); // 如果没有指派任何栏目,则为0，同样屏蔽所有栏目输出
          }

         if(rid>=0 && all==null){
           m.put("rid", rid);   
         }
         if(isopen>=0){
             m.put("isopen", isopen);
         }
         if(infotype>=0){
             m.put("infotype", infotype);
         }

         if(ordnum!=null){
             m.put("ordnum", "ordnum");
         }
         if(colfilter!=null){
             m.put("colfilter", "colfilter");
         }
         List list = CatalogsService.listByFactor(m);
         if(ztree==null){
            Map model = new HashMap();
                model.put("list", list);
           return ModelUtil.getJsonModel(model);
         }else{
            return ModelUtil.getJsonModel(list);
         }
     }
     
     
     public ModelView delete(HttpServletRequest req)throws Exception{
         int cid = WebUtil.getInt(req, "cid", -1);
         int rid = WebUtil.getInt(req, "rid", -1);
         if(cid<0 || rid<0){
             return ModelUtil.getTextModel(Message.MSG_PARAM_ERR);
         }
         if(CatalogsService.deleteFinal(cid, rid)){
            return ModelUtil.getTextModel(Message.MSG_SUCCESS);
         }else{
            return ModelUtil.getTextModel(Message.MSG_FAIL);
         }
     }
     
     
     public ModelView update_base(HttpServletRequest req)throws Exception{
         int cid = WebUtil.getInt(req, "cid", -1);
         byte isopen = WebUtil.getByte(req, "isopen", -1);
         String cname = WebUtil.getTitle(req, "cname", null);
         int ordnum = WebUtil.getInt(req, "ordnum", -1);
         if(cid<0){
             return null;
         }
         boolean flag = false;
         if(isopen>=0){
             flag = CatalogsService.updateIsOpen(isopen, cid) ;
         }else if(cname!=null){
             flag = CatalogsService.updateCname(cname, cid) ;
         }else if(ordnum>=0){
             flag = CatalogsService.updateOrdnum(ordnum, cid) ;
         }
        if(flag){
          return ModelUtil.getTextModel(Message.MSG_SUCCESS);
        }else{
          return ModelUtil.getTextModel(Message.MSG_FAIL);  
        }
     }
     
     
     public ModelView catalogs_num(){
         return ModelUtil.getTextModel(CatalogsService.getCatalogsNum());
     }
    
}
